<?php
/**
 * OneBase 安装程序
 * @author 王勇程 <laughskydragon@qq.com>
 * @date 2017-02-10
 */

namespace app\install\controller;

use app\common\controller\ControllerBase;
use think\facade\Db;
use think\facade\Request;
use app\install\logic\Install;

class Index extends ControllerBase
{
    
    /**
     * 构造方法
     */
    public function __construct(\think\App $app, \think\Request $request)
    {
        
        // 执行父类构造方法
        parent::__construct($app, $request);
        
        'complete' != $this->request->action() && $this->checkInstall();
    }
    
    /**
     * 检查是否已安装
     */
    public function checkInstall()
    {
        
        if ($this->installedFlagExists()) {
            $this->error('OneBase已经成功安装，请勿重复安装!');
        }
    }

    /**
     * 安装引导首页
     */
    public function index()
    {
        
        if ($this->installedFlagExists()) {
            $this->error('系统已安装，如需重新安装，请清空或重置 config/database.php 中的数据库配置');
        }
        
        return $this->fetch();
    }
    
    /**
     * 安装成功页
     */
    public function complete()
    {
        
        return $this->fetch('complete');
    }
    
    /**
     * 检测运行所需的环境设置
     */
    public function step1()
    {
        
        if ($this->installedFlagExists()) {
            $this->error('系统已安装，如需重新安装，请清空或重置 config/database.php 中的数据库配置');
        }
        
        $data = [];
        
        $data['env'] = check_env();
        
        $data['dirfile'] = check_dirfile();
        
        $data['func'] = check_func();
        
        // 关键修改：将数组键展开为模板顶层变量，避免 {$env} 未定义
        $this->assign($data);
        
        return $this->fetch();
    }
    
    /**
     * 安装数据写入
     */
    public function step2()
    {
        
        if ($this->installedFlagExists()) {
            $this->error('系统已安装，如需重新安装，请清空或重置 config/database.php 中的数据库配置');
        }
        
        if ($this->request->isPost()) {
            
            $data = $this->request->post();
            
            $install_logic = new Install();
            
            $check_result = $install_logic->checkData($data);
            
            if ($check_result['code']) {
                
                $install_result = $install_logic->install($data);
                
                if ($install_result['code']) {
                    
                    $this->success($install_result['msg'], '/index/index');
                    
                } else {
                    
                    $this->error($install_result['msg']);
                }
                
            } else {
                
                $this->error($check_result['msg']);
            }
        }
        
        return $this->fetch();
    }

    /**
     * 以 config/database.php 的内容作为唯一安装标识
     */
    protected function installedFlagExists(): bool
    {
        $cfgFile = app()->getRootPath() . 'config' . DIRECTORY_SEPARATOR . 'database.php';
        if (!is_file($cfgFile)) {
            return false;
        }
        $cfg = include $cfgFile;
        if (!is_array($cfg)) {
            return false;
        }
        // 检测关键项：数据库名不为空且密钥不为默认值
        $dbName = $cfg['connections']['mysql']['database'] ?? '';
        $sysKey = $cfg['sys_data_key'] ?? 'OneBase';
        return ($dbName !== '' && $sysKey !== 'OneBase');
    }
}
